package com.clouddrive.service;

import com.clouddrive.User;
import com.clouddrive.dao.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value = "/userlogout",name = "UserLogout")
public class UserLogout extends HttpServlet {
    User nowUser=new User();
    UserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String useremail=request.getParameter("email");
//        System.out.println(useremail);

        Cookie[] cookies=request.getCookies();
        nowUser=userService.GetUserByCookie(cookies);

        if(nowUser.getEmail().equals(useremail)){
            for(Cookie cookie:cookies){
                Cookie newcookie=new Cookie(cookie.getName(),"");
                newcookie.setPath("/MyCloudDrive");
                //TODO:往服务器上丢war包前请修改此处，将setDomain的内容修改为对应ip地址
                newcookie.setDomain("localhost");
                //newcookie.setDomain("43.138.39.84");
                newcookie.setMaxAge(0);
                response.addCookie(newcookie);
            }
//            Cookie[] cookies1=request.getCookies();
//            System.out.println("删除完cookie "+cookies1.length);
//            for(Cookie cookie:cookies1){
//                System.out.println(cookie.getName()+" "+cookie.getValue());
//            }
            request.getSession().invalidate();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
